﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;
using System.Data;

namespace Sispe.Repository
{
    public class EstudioRepository:MasterRepository, IEstudioRepository
    {
        public List<ESTUDIO> GetAllFromEstudios()
        {
            var query = from s in context.ESTUDIOS.Include("GRADO")
                        select s;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.ToList();
        }

        public List<ESTUDIO> GetAllFromEstudiosByDescripcion(string descripcion)
        {
            var query = from s in context.ESTUDIOS.Include("GRADO")
                        select s;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from s in context.ESTUDIOS
                        where s.Descripcion.Contains(descripcion) ||
                                  s.Codigo.ToUpper().Contains(descripcion)
                        select s;
            }
            return query.ToList();
        }

        public ESTUDIO GetFromEstudioById(int id)
        {
            var query = from s in context.ESTUDIOS.Include("GRADO")
                        where s.IdEstudios.Equals(id)
                        select s;
            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.SingleOrDefault();
        }

       

        public void AddEstudios(ESTUDIO estudios)
        {
            estudios.GRADO = null;
            estudios.TRABAJADORs = null;
            context.ESTUDIOS.Add(estudios);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdateEstudios(ESTUDIO estudios)
        {
            estudios.GRADO = null;
            estudios.TRABAJADORs = null;
            context.ESTUDIOS.Attach(estudios);
            context.Entry(estudios).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void RemoveEstudios(int id)
        {
            var existe = context.ESTUDIOS.Find(id);

            if (existe != null)
            {
                context.ESTUDIOS.Remove(existe);
                context.SaveChanges();
            }
        }


        public bool ExisteDuplicidad(string descripcion, string codigo, int id)
        {
            var query = from s in context.ESTUDIOS
                        where (s.Descripcion.CompareTo(descripcion) == 0 &&
                                  s.Codigo.ToUpper().CompareTo(codigo.ToUpper()) == 0)
                                  && s.IdEstudios !=id
                        select s;
            return query.Any();
        }


        public bool GetFromEstudioByGradoById(int idGrado)
        {
            var query = from e in context.ESTUDIOS.Include("GRADO")
                        where e.IdGrado.Equals(idGrado)
                        select e;
            return query.Any();
        }
    }
}
